<script setup lang="ts">
import {ref} from 'vue'
import {formatterScore} from "@/utils/score";
import {getRankingList, User} from "@/api";
import BOY_AVATAR_A from '@/assets/avatar/BOY_AVATAR_A.svg'

// 用户数据
const users = ref<User[]>([])

// 控制弹窗显示
const visible = ref(false)

// 计算进度条百分比
const calculateProgress = (score: number) => {
  const maxScore = Math.max(...users.value.map(u => u.score))
  return (score / maxScore) * 100
}

const open = async () => {
  visible.value = true
  try {
    const res = await getRankingList()
    users.value = res.data
  } finally {

  }
  document.body.style.overflow = 'hidden'
}

const close = () => {
  visible.value = false
  document.body.style.overflow = ''
}

defineExpose({
  open,
  close
})
</script>

<template>
  <div class="ranking-modal" :class="{ active: visible }" @click.self="close">
    <div class="ranking-container">
      <div class="ranking-header">
        <div>🍃 排行榜</div>
        <div class="prompt">
          <div>提示：设置昵称后才能参与排名</div>
          <div class="duration">排名保留 7 天</div>
        </div>
        <button class="close-btn" @click="close">
          <img src="@/assets/icons/close.svg" alt="关闭">
        </button>
      </div>
      <div class="ranking-list">
        <div
            v-for="(user, index) in users"
            :key="index"
            class="ranking-item"
            :class="{
            'top-3': index < 3,
            'rank-1': index === 0,
            'rank-2': index === 1,
            'rank-3': index === 2
          }"
        >
          <div class="rank-number">{{ index + 1 }}</div>
          <img :src="user.avatar || BOY_AVATAR_A" :alt="user.nickname" class="user-avatar">
          <div class="user-info">
            <div class="user-nickname">
              <span>{{ user.nickname }}</span>
              <span v-if="user.isSelf" class="self-label">@自己</span>
            </div>
            <div class="user-score">
              {{ formatterScore(user.score) }}
            </div>
            <div class="score-bar">
              <div
                  class="score-progress"
                  :style="{ width: calculateProgress(user.score) + '%' }"
              ></div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.ranking-modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: rgba(0, 0, 0, 0.7);
  z-index: 1000;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease;
}

.ranking-modal.active {
  opacity: 1;
  pointer-events: all;
}

.ranking-container {
  max-width: 550px;
  width: 95%;
  border-radius: 5px;
  overflow: hidden;
  background-color: #fff;
  transform: scale(0.9);
  transition: transform 0.3s ease;
}

.ranking-modal.active .ranking-container {
  transform: scale(1);
}

.ranking-header {
  padding: 10px 20px;
  text-align: left;
  color: #1f1f1f;
  position: relative;
  font-size: 17px;
  border-bottom: 1px solid #eee;
  display: flex;
  align-items: center;

  .prompt {
    font-size: 14px;
    color: #888;
    margin-left: 16px;
    .duration {
      margin-top: 5px;
      font-size: 12px;
      color: #3498db;
    }
  }
}

.close-btn {
  position: absolute;
  right: 20px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;

  img {
    width: 20px;
    height: 20px;
  }
}

.ranking-list {
  padding: 10px;
  max-height: 80vh;
  min-height: 100px;
  overflow-y: auto;
}

.ranking-item {
  display: flex;
  align-items: center;
  padding: 8px 12px;
  margin-bottom: 8px;
  border-radius: 5px;
  transition: all 0.3s ease;
  overflow: hidden;
  border: 1px solid #eee;
  user-select: none;
}

.ranking-item:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.rank-number {
  width: 50px;
  color: #1f1f1f;
  text-align: center;
  margin-right: 8px;
}

.top-3 .rank-number {
  font-size: 20px;
  font-weight: 700;
}

.rank-1 .rank-number {
  color: #ffd700;
}

.rank-2 .rank-number {
  color: #c0c0c0;
}

.rank-3 .rank-number {
  color: #cd7f32;
}

.user-avatar {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  object-fit: cover;
  border: 2px solid #e0e0e0;
  margin-right: 20px;
  transition: all 0.3s ease;
}

.ranking-item:hover .user-avatar {
  transform: scale(1.1);
  border-color: #3498db;
}

.user-info {
  flex: 1;
}

.user-nickname {
  color: #1f1f1f;
  font-size: 14px;

  .self-label {
    color: #0081ff;
    margin-left: 3px;
  }
}

.user-score {
  display: flex;
  align-items: center;
  font-size: 13px;
  margin: 5px 0;
  color: #0081ff;
}

.score-bar {
  height: 6px;
  background-color: #f0f0f0;
  border-radius: 4px;
  overflow: hidden;
}

.score-progress {
  height: 100%;
  background: linear-gradient(to right, #3498db, #21b4ff);
  border-radius: 4px;
  transition: width 0.5s ease;
}

.top-3 {
  background: linear-gradient(to right, rgba(236, 240, 241, 0.6), white);
}

.rank-1 {
  background: linear-gradient(to right, rgba(255, 215, 0, 0.1), white);
}

.rank-2 {
  background: linear-gradient(to right, rgba(192, 192, 192, 0.1), white);
}

.rank-3 {
  background: linear-gradient(to right, rgba(205, 127, 50, 0.1), white);
}

@media (max-width: 480px) {
  .ranking-container {
    width: 95%;
  }

  .user-avatar {
    width: 40px;
    height: 40px;
    margin-right: 10px;
  }

  .rank-number {
    width: 40px;
  }
}
</style>